namespace SharpPlanetsModel {

  public abstract class Planet: MassiveSpaceObject, IPlanet {

    public Planet() {
      radius = 0;
      mass = 0;
      density = 12;
    }

    /// <summary>
    /// Propriet� che definisce il raggio del pianeta, espresso nelle dimensioni
    /// assolute (ovvero indipendenti dalla visualizzazione corrente)
    /// </summary>
    public int Radius {
      get {
        return radius;//throw new System.NotImplementedException();
      }
      set {   // inserire eventuali controlli....
        radius = value;
        Calculate();
      }
    }

    protected int radius;
    protected System.Drawing.Image planetImage;

    protected void Calculate() {
      mass = density * radius * radius * radius / Configuration.densityFactor;
    }

    /// <summary>
    /// metodo astratto che associa l'immagine al pianeta
    /// </summary>
    abstract protected void setupImage();

    ////public bool Collide(Position pos)
    ////{
    ////    int Xdistance = this.CenterPosition.getXAbs() - pos.getXAbs();
    ////    int Ydistance = this.CenterPosition.getYAbs() - pos.getYAbs();
    ////    double distance = System.Math.Sqrt(System.Math.Pow(Xdistance, 2) + System.Math.Pow(Ydistance, 2));
    ////    if (distance > radius)
    ////    {
    ////        return false;
    ////    }
    ////    else
    ////    {
    ////        return true;
    ////    }
    ////}

    override public void Paint(System.Drawing.Graphics graph) {
      if (planetImage != null) {

        int ray = (int)((radius * Configuration.fieldWidthInPixels) / Configuration.drawingFactor );
        int hlx = this.CenterPosition.getXRel(Configuration.fieldWidthInPixels) - ray;
        int hly = this.CenterPosition.getYRel(Configuration.fieldWidthInPixels) - ray;

        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(hlx, hly, 2 * ray, 2 * ray);
        System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(0, 0, 100, 100);
        System.Drawing.GraphicsUnit myUnits = System.Drawing.GraphicsUnit.Pixel;
        graph.DrawImage(planetImage, destRect, srcRect, myUnits);

        //////System.Drawing.Brush myBrush = new System.Drawing.TextureBrush(planetImage);
        //////graph.FillEllipse(myBrush, hlx, hly, ray * 2, ray * 2);

        //////System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Yellow);
        //////graph.DrawEllipse(myPen, hlx, hly, ray * 2, ray * 2);
      }
    }

    //public bool checkCollision(double x, double y)
    //{
    //    double dx2, dy2;
    //    dx2 = System.Math.Pow((this.CenterPosition.getXAbs() - x), 2);
    //    dy2 = System.Math.Pow((this.CenterPosition.getYAbs() - y), 2);
    //    if ((dx2 + dy2) <= System.Math.Pow(radius, 2))
    //        return (true);
    //    else
    //        return (false);
    //}

    /// <summary>
    /// check if a given point is external to the planet or collide with it
    /// </summary>
    /// <param name="p">position of the point</param>
    /// <returns>true if there's a collision, false otherwise</returns>
    public bool Collide(Position p) {
      double dx2, dy2;
      dx2 = System.Math.Pow((this.CenterPosition.getXAbs() - p.getXAbs()), 2);
      dy2 = System.Math.Pow((this.CenterPosition.getYAbs() - p.getYAbs()), 2);
      if ((dx2 + dy2) <= System.Math.Pow(radius, 2))
        return (true);
      else
        return (false);
    }

  }
}
